home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 615 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.7 KB  |  96 lines

  1. Path: tbj.dec.com!diamond
  2. From: diamond@tbj.dec.com (Norman Diamond)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: 21 Mar 1996 03:44:27 GMT
  6. Organization: Digital Equipment Corporation Japan , Tokyo
  7. Message-ID: <4iqjar$2m9@usenet.pa.dec.com>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk>
  9. Reply-To: diamond@tbj.dec.com (Norman Diamond)
  10. NNTP-Posting-Host: jit533.tbj.dec.com
  11.  
  12. In article <4iokop$h4p@lyra.csx.cam.ac.uk>, jkb@mrc-lmb.cam.ac.uk (James Bonfield) writes:
  13. >Are there any limitations on what the sort function passed over to qsort can
  14. >do or return?
  15.  
  16. Another followup stated a condition which is probably correct (the standard
  17. isn't exactly explicit about it) but you are obeying that condition so it
  18. is not your problem.
  19.  
  20. >I know it's meant to return < 0, 0 or > 0 for the various compare operations,
  21. >but which you return is purely up to your own comparison system.
  22.  
  23. Exactly.
  24.  
  25. >On tracking down a bug in some old code I noticed that we had the
  26. >compare function returning something like "a > b" instead of "b - a".
  27. >Now this is obviously some silly bug in our coding,
  28.  
  29. Actually it's a silly question.  Just one sentence earlier, you gave the
  30. exact reason why it doesn't matter if you do "a > b" instead of "a - b".
  31. (Actually it can matter:  if "a - b" would overflow then doing "a > b"
  32. will fix it instead of yielding undefined behavior :-)
  33. On the other hand, doing "a > b" instead of "a < b" reverses the order
  34. of the result -- no difference in technical validity, but likely only
  35. one of these matches your requirement.
  36.  
  37. >such functions appear to make [one implementation's] qsort() function
  38. >underflow the passed array.
  39.  
  40. This should not happen.
  41.  
  42. >#include <stdio.h>
  43. >#include <stdlib.h>
  44. >static int sort_func(const void *pa, const void *pb) {
  45. >    const int *a = (int *)pa;
  46. >    const int *b = (int *)pb;
  47. >    return *a > *b;
  48. >}
  49.  
  50. Incidentally, if you returned "a > b" instead of "*a > *b" then you would
  51. likely be returning inconsistent results, violating the probably correct
  52. condition which someone else posted.
  53.  
  54. >#define NUM_ELE 10
  55. >int main() {
  56. >    int i;
  57. >    int crashme;     /* removing this line fixes things! */
  58.  
  59. This makes me suspicious that your test program is not exactly what you
  60. posted, and your test program has a bug in some other part of its code
  61. that already yielded undefined behavior.
  62.  
  63. >    int sortme[NUM_ELE];
  64. >    srand(time(NULL));
  65.  
  66. Or it might be this bug right here.  The standard library function time()
  67. returns type time_t which might not be int.  Your main() function assumes
  68. that it must be int.  You can fix this by adding "#include <time.h>".
  69.  
  70. >    for (i=0; i<NUM_ELE; i++) {
  71. >        sortme[i] = rand()%100+50;
  72. >    }
  73. >    qsort((void *)sortme, NUM_ELE, sizeof(int), sort_func);
  74. >    return 0;
  75. >}
  76. >Adding some debugging information to this and printing up the array
  77. >before and after sorting (including the values (666) immediately above
  78. >and below the array) shows that the contents of memory outside of the
  79. >array actually get swapped with memory inside the array. I can make it
  80. >overflow too.
  81.  
  82. You probably have a bug in the other code that you haven't shown.
  83. There's no 666 in the code you've shown.
  84.  
  85. >My understanding of this is that qsort() ought to be able to handle any
  86. >sort function, even if it's something as dumb as (rand()%3)-1.
  87.  
  88. That would not be a sort function (it might even assert that a < b and
  89. b < c and c < a).  As for whether qsort() is required to contend with
  90. such nonsense, it "probably" isn't.
  91. --
  92.  <<  If this were the company's opinion, I would not be allowed to post it.  >>
  93. "I paid money for this car, I pay taxes for vehicle registration and a driver's
  94. license, so I can drive in any lane I want, and no innocent victim gets to call
  95. the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
  96.